import { Controller, Get, Post, Body, Put, Param, Delete, Request, Query, Res } from '@nestjs/common';
import { Response } from 'express';
import { ApiTags, ApiOperation, ApiBody, ApiBearerAuth, ApiParam } from '@nestjs/swagger';
import { ConfigService } from './config.service';
import { CreateConfigDto, UpdateConfigDto, ListConfigDto } from './dto/index';
import { RequirePermission } from 'src/common/decorators/require-premission.decorator';
import { RepeatSubmit } from 'src/common/decorators/repeat-submit.decorator';
import { Operlog } from 'src/common/decorators/operlog.decorator';
import { BusinessType } from 'src/common/constant/business.constant';
import { UserTool, UserToolType } from '../user/user.decorator';

@ApiTags('参数设置')
@ApiBearerAuth('token')
@Controller('system/config')
export class ConfigController {
  constructor(private readonly configService: ConfigService) {}

  @ApiOperation({ summary: '参数设置创建' })
  @ApiBody({ type: CreateConfigDto, required: true })
  @RequirePermission('system:config:add')
  @RepeatSubmit()
  @Operlog({ businessType: BusinessType.INSERT })
  @Post()
  create(@Body() createConfigDto: CreateConfigDto, @UserTool() { injectCreate }: UserToolType) {
    return this.configService.create(injectCreate(createConfigDto));
  }

  @ApiOperation({ summary: '参数设置列表' })
  @ApiBody({ type: ListConfigDto, required: true })
  @RequirePermission('system:config:list')
  @Get('/list')
  findAll(@Query() query: ListConfigDto) {
    return this.configService.findAll(query);
  }

  @ApiOperation({ summary: '参数设置详情' })
  @RequirePermission('system:config:query')
  @Get(':id')
  @ApiParam({ name: 'id', required: true, description: '配置ID' })
  findOne(@Param('id') id: string) {
    return this.configService.findOne(+id);
  }

  @ApiOperation({ summary: '参数设置详情(configKey)【走缓存】' })
  @RequirePermission('system:config:query')
  @Get('/configKey/:configKey')
  @ApiParam({ name: 'configKey', required: true, description: '配置键名' })
  findOneByConfigKey(@Param('configKey') configKey: string) {
    return this.configService.findOneByConfigKey(configKey);
  }

  @ApiOperation({ summary: '参数设置更新' })
  @ApiBody({ type: UpdateConfigDto, required: true })
  @RequirePermission('system:config:edit')
  @RepeatSubmit()
  @Operlog({ businessType: BusinessType.UPDATE })
  @Put()
  update(@Body() updateConfigDto: UpdateConfigDto, @UserTool() { injectUpdate }: UserToolType) {
    return this.configService.update(injectUpdate(updateConfigDto));
  }

  @ApiOperation({ summary: '参数设置刷新缓存' })
  @RequirePermission('system:config:remove')
  @Delete('/refreshCache')
  @Operlog({ businessType: BusinessType.CLEAN })
  refreshCache() {
    return this.configService.resetConfigCache();
  }

  @ApiOperation({ summary: '参数设置删除' })
  @RequirePermission('system:config:remove')
  @Delete(':id')
  @Operlog({ businessType: BusinessType.INSERT })
  @ApiParam({ name: 'id', required: true, description: '配置ID' })
  remove(@Param('id') ids: string) {
    const configIds = ids.split(',').map((id) => +id);
    return this.configService.remove(configIds);
  }

  @ApiOperation({ summary: '参数设置导出参数管理为xlsx文件' })
  @ApiBody({ type: ListConfigDto, required: true })
  @RepeatSubmit()
  @RequirePermission('system:config:export')
  @Operlog({ businessType: BusinessType.EXPORT })
  @Post('/export')
  async export(@Res() res: Response, @Body() body: ListConfigDto): Promise<void> {
    return this.configService.export(res, body);
  }
}
